package com.sherllinhe.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.sherllinhe.common.context.LoginMemberContext;
import com.sherllinhe.business.domain.TrainCarriage;
import com.sherllinhe.business.enums.SeatColEnum;
import com.sherllinhe.common.resp.PageResp;
import com.sherllinhe.common.util.SnowUtil;
import com.sherllinhe.business.domain.TrainSeat;
import com.sherllinhe.business.domain.TrainSeatExample;
import com.sherllinhe.business.mapper.TrainSeatMapper;
import com.sherllinhe.business.req.TrainSeatQueryReq;
import com.sherllinhe.business.req.TrainSeatSaveReq;
import com.sherllinhe.business.resp.TrainSeatQueryResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能：
 * 作者：Sherllin He
 * 日期：
 */
@Slf4j
@Service
public class TrainSeatService {
    @Autowired
    private TrainSeatMapper trainSeatMapper;
    @Autowired
    private TrainCarriageService trainCarriageService;
    // 新增和编辑乘客
    public void save(TrainSeatSaveReq req){
        DateTime now = DateTime.now();
        TrainSeat trainSeat = BeanUtil.copyProperties(req, TrainSeat.class);

        //判断是新增还是更新
        //trainSeat.setMemberId(LoginMemberContext.getId());
        if(ObjUtil.isNull(trainSeat.getId())) {
            //新增
            trainSeat.setId(SnowUtil.getSnowflakeNextId());
            trainSeat.setCreateTime(now);
            trainSeat.setUpdateTime(now);
            trainSeatMapper.insert(trainSeat);
        }else{
            //更新
            trainSeat.setUpdateTime(now);
            trainSeatMapper.updateByPrimaryKey(trainSeat);
        }


    }
    // 查询乘客列表
    public PageResp<TrainSeatQueryResp> queryList(TrainSeatQueryReq req){
        TrainSeatExample example = new TrainSeatExample();
        example.setOrderByClause("train_code asc,carriage_index asc,carriage_seat_index asc");
        TrainSeatExample.Criteria criteria = example.createCriteria();
        //if(ObjUtil.isNotNull(req.getMemberId())){
            //criteria.andMemberIdEqualTo(req.getMemberId());
        //}

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<TrainSeat> trainSeatList = trainSeatMapper.selectByExample(example);

        PageInfo<TrainSeat> pageInfo = new PageInfo<>(trainSeatList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<TrainSeatQueryResp> list = BeanUtil.copyToList(trainSeatList, TrainSeatQueryResp.class);

        PageResp<TrainSeatQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }
    //删除乘客
    public void delete(Long id){
        trainSeatMapper.deleteByPrimaryKey(id);
    }

    //生成座位信息
    @Transactional
    public void genTrainSeat(String trainCode){
        DateTime  now = DateTime.now();
        //清空座位信息
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.createCriteria().andTrainCodeEqualTo(trainCode);
        trainSeatMapper.deleteByExample(trainSeatExample);
        //查找当前车次下的所有车厢
        List<TrainCarriage> carriageList = trainCarriageService.selectByTrainCode(trainCode);
        log.info("当前车次:{}，车厢数:{}", trainCode, carriageList.size());
        //循环生成每个车厢的座位信息
        for (TrainCarriage carriage : carriageList) {
            //拿到车厢行数、座位类型
            Integer rowCount = carriage.getRowCount();
            String seatType = carriage.getSeatType();
            int seatIndex = 1;
            //根据车箱的座位类型，筛选出所有的列，比如车箱类型是一等座，则筛选出columnList={ACDF}
            List<SeatColEnum> colEnumList = SeatColEnum.getColsByType(seatType);
            //循环生成座位信息
            for (int row = 1; row <= rowCount; row++) {
                for (SeatColEnum colEnum : colEnumList){
                    TrainSeat trainSeat = new TrainSeat();
                    trainSeat.setId(SnowUtil.getSnowflakeNextId());
                    trainSeat.setTrainCode(trainCode);
                    trainSeat.setCarriageIndex(carriage.getIndex());
                    trainSeat.setRow(StrUtil.fillBefore(String.valueOf(row),'0',2));
                    trainSeat.setCol(colEnum.getCode());
                    trainSeat.setSeatType(seatType);
                    trainSeat.setCarriageSeatIndex(seatIndex++);
                    trainSeat.setCreateTime(now);
                    trainSeat.setUpdateTime(now);

                    trainSeatMapper.insert(trainSeat);
                }
            }
        }
    }

    //查询某车次的座位信息
    public PageResp<TrainSeatQueryResp> selectByTrainCode(String trainCode, TrainSeatQueryReq req){
        TrainSeatExample example = new TrainSeatExample();
        TrainSeatExample.Criteria criteria = example.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<TrainSeat> trainSeatList = trainSeatMapper.selectByExample(example);

        PageInfo<TrainSeat> pageInfo = new PageInfo<>(trainSeatList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<TrainSeatQueryResp> list = BeanUtil.copyToList(trainSeatList, TrainSeatQueryResp.class);

        PageResp<TrainSeatQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }
    public List<TrainSeat> selectByUnique(String trainCode){
        TrainSeatExample example = new TrainSeatExample();
        example.setOrderByClause("carriage_index, carriage_seat_index");
        TrainSeatExample.Criteria criteria = example.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        return trainSeatMapper.selectByExample(example);
    }
}
